﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class admin_companylist : System.Web.UI.Page
{
    public DataTable Loginarr = null;
    rUser ruser = null;
    DataTable Agentarr = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            rUser ruser = null;
            if (Session["userid"] != null)
            {
                ruser = (rUser)Session["userid"];
                string stactive = "";




           

            Loginarr = (DataTable)DB.getArrLoginAcc("");
           
            fillcompanies();

            Agentarr = (DataTable)DB.getArrAgent("");
            gv_agent.DataSource = Agentarr;
            gv_agent.DataBind();
            fillagents(Agentarr);
            ArrayList userpage = new ArrayList();
            if (Application["userPage"] == null) {

                Application.Set("userPage",userpage);

            }
            userpage = (ArrayList)Application["userPage"];
           

            int encontro = 1;
           
            foreach (RE_GenericBean rg in userpage)
            {

                if (rg.strC1.Equals(ruser.getAlias()))
                {
                    encontro = encontro * 0;
                }
                else {
                    if (rg.strC2.Equals("login"))
                    {
                        stactive += "&nbsp; &nbsp; <img src= '../img/user_online.png' alt='' width='10'> &nbsp; " + rg.strC1;
                    }
                
                }
               

                
            
            }
            if (encontro == 1) {
                RE_GenericBean r = new RE_GenericBean();
                r.strC1 = ruser.getAlias();
                r.strC2 = "login";
                userpage.Add(r);
                Application.Remove("userPage");
                Application.Set("userPage", userpage);
            }

            
               // lb_users.Text = stactive;
            }
            
           
            
        }
    
    }

    private void fillagents(DataTable Agentarr)
    {
        DropDownList1.Items.Clear();
        DropDownList2.Items.Clear();
        DropDownList1.Items.Add("Select One");
        DropDownList2.Items.Add("Select One");
        for (int ii = 0; ii <= (Agentarr.Rows.Count - 1); ii++)
        {
            ListItem listitem = new ListItem();
            ListItem listmail = new ListItem();
            listitem.Text = Agentarr.Rows[ii][1].ToString();
            listitem.Value = Agentarr.Rows[ii][0].ToString();
            listmail.Value = Agentarr.Rows[ii][0].ToString();
            listmail.Text = Agentarr.Rows[ii][2].ToString();
            DropDownList1.Items.Add(listitem);
            DropDownList2.Items.Add(listmail);





        }
    }


    protected void Page_Init(object sender, EventArgs e) {

        Loginarr = (DataTable)DB.getArrLoginAcc("");
        
        fillaccordion(Loginarr);
    }
    
    protected void gv_multiple_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
   
    protected void lnk_save_Click(object sender, EventArgs e)
    {

       /* img_error.Visible = false;
        img_ok.Visible = false;
        ArrayList array = new ArrayList();
       
        foreach (GridViewRow ROW in gv_multiple.Rows)
        {
            
                int con = ROW.Controls.Count;
                Label tbid = (Label)ROW.FindControl("lbid");
                TextBox tbcom = (TextBox)ROW.FindControl("lbcompany");
                TextBox tbname = (TextBox)ROW.FindControl("lbname");
                TextBox tbuser = (TextBox)ROW.FindControl("lbuser");
                TextBox tbpass = (TextBox)ROW.FindControl("lbpass");
                TextBox tbmail = (TextBox)ROW.FindControl("lbmail");
                TextBox tbadd = (TextBox)ROW.FindControl("lbaddress");
                TextBox tbphone = (TextBox)ROW.FindControl("lbphone");
                TextBox tblicense = (TextBox)ROW.FindControl("lblicense");
                TextBox tbexp = (TextBox)ROW.FindControl("lbexpiration");
                TextBox tbeo = (TextBox)ROW.FindControl("lbeo");
                TextBox tbexp2 = (TextBox)ROW.FindControl("lbexpiration2");
                TextBox tbw9 = (TextBox)ROW.FindControl("lbw9");

                            
                             

                    RE_GenericBean rg = new RE_GenericBean();
                    rg.intC1 = int.Parse(tbid.Text);
                    rg.strC1 =tbcom.Text;
                    rg.strC2 = tbname.Text;
                    rg.strC3 = tbuser.Text;
                    rg.strC4 = tbpass.Text;
                    rg.strC5 = tbmail.Text;
                    rg.strC6 = tbadd.Text;
                    rg.strC7 = tbphone.Text;
                    rg.strC8 = tblicense.Text;
                    rg.strC9 = tbexp.Text;
                    rg.strC10 = tbeo.Text;
                    rg.strC11 = tbexp2.Text;
                    rg.strC12 = tbw9.Text;                    
                    array.Add(rg);
                    



            }

        
        

        RE_GenericBean resop = new RE_GenericBean();
        string user = "";

        if (Session["userid"] == null)
        {
            ruser = (rUser)Session["userid"];
            user = ruser.getAlias();

        }
        if (array.Count > 0) { resop = DB.saveLogin(array, user); }
      
        if (resop.intC1 == 1)
        {
            lb_error.Text = "Change applied";
            lb_error.Visible = true;
            img_ok.Visible = true;
            


        }
        else
        {
            lb_error.Text = "Change not applied:  " + resop.strC1;
            lb_error.Visible = true;
            img_error.Visible = true;

        }   
         */  
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Loginarr = (DataTable)DB.getArrLoginAcc("");
        fillaccordion(Loginarr);
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {

    }

    protected void editCompany(object sender, EventArgs e) {
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        int index = objFila.RowIndex;


        int i = 0;


        foreach (GridViewRow ROW in gv_company.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
                Label id = (Label)ROW.FindControl("lbid");
                Label tb = (Label)ROW.FindControl("lburl");
                Label tb2 = (Label)ROW.FindControl("lbphone");
                Label tb3 = (Label)ROW.FindControl("lbcontacts");
                Label tb4 = (Label)ROW.FindControl("lbcompany");

                lbidc.Text = id.Text;
                txturl.Text = tb.Text;
                tb_phone2.Text = tb2.Text;
                tb_contacts.Text = tb3.Text;
                txtcompany.Text = tb4.Text;
            }
            i++;
        }



        ModalPopupExtender1.Show();
        
    
    }

    protected void edit(object sender, EventArgs e) {
        
        ImageButton bt = (ImageButton)sender;
        string name = bt.ID;
        string[] id = name.Split('_');
        DataTable l = (DataTable)DB.getArrLoginAcc("  l.id = "+id[1]);

        //colocando la info en el popup
        lbid.Text = l.Rows[0][0].ToString();
        string t = l.Rows[0][1].ToString();
       cbcompany.SelectedValue = l.Rows[0][13].ToString();
        DropDownList1.SelectedValue = l.Rows[0][14].ToString();
        DropDownList2.SelectedValue = l.Rows[0][14].ToString();
        tb_mailogin.Text = DropDownList2.SelectedItem.ToString();
        tb_user.Text = l.Rows[0][3].ToString();
        tb_pass.Text = l.Rows[0][4].ToString();
        tb_add.Text = l.Rows[0][6].ToString();
        tb_phone.Text = l.Rows[0][7].ToString();
        tb_license.Text = l.Rows[0][8].ToString();
        tb_exp.Text = l.Rows[0][9].ToString();
        tb_eo.Text = l.Rows[0][10].ToString();
        tb_exp2.Text = l.Rows[0][11].ToString();
        tb_w.Text = l.Rows[0][12].ToString();

        PMsg_ModalPopupExtender.Show();
    }
    protected void editAgent(object sender, EventArgs e)
    {

        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        int index = objFila.RowIndex;


        int i = 0;


        foreach (GridViewRow ROW in gv_agent.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
                Label id = (Label)ROW.FindControl("lbidag");
                Label tb = (Label)ROW.FindControl("lbnameag");
                Label tb2 = (Label)ROW.FindControl("lbemailag");


                lbidagent.Text = id.Text;
                tbnameag.Text = tb.Text;
                tbemailag.Text = tb2.Text;
                
            }
            i++;
        }


        ModalPopupExtender2.Show();

        
    }
    protected void delete(object sender, EventArgs e)
    {
      

              
               
                RE_GenericBean rg = new RE_GenericBean();
                rg.intC1 = int.Parse(lbid.Text);
                int resp = DB.deleteLoginAcc(rg.intC1);
                if (resp == 1)
                {
                    lb_error.Text = "Item Deleted";
                    lb_error.Visible = true;
                    lb_error.ForeColor = System.Drawing.Color.Blue;
                    img_ok.Visible = true;
                }
                else {
                    lb_error.Text = "Cant be delete:  ";
                    lb_error.Visible = true;
                    lb_error.ForeColor = System.Drawing.Color.Red;
                    img_error.Visible = false;
                }
                    


        Loginarr = (DataTable)DB.getArrLoginAcc("");
        fillaccordion(Loginarr);
        cbcompany.SelectedIndex = 0;
       
        tb_user.Text = "";
        tb_pass.Text = "";
        tb_mailogin.Text = "";
        tb_add.Text = "";
        tb_phone.Text = "";
        tb_license.Text = "";
        tb_exp.Text = "";
        tb_eo.Text = "";
        tb_exp2.Text = "";
        tb_w.Text = "";
        lbid.Text = "0";
        Response.Redirect("companylist.aspx");
    }
    protected void lnk_msg_ok_Click(object sender, EventArgs e)
    {
        cbcompany.SelectedIndex = -1;
        DropDownList1.SelectedIndex = -1;
        DropDownList2.SelectedIndex = -1;
        tb_user.Text = "";
        tb_pass.Text = "";
        tb_exp2.Text = "";
        tb_exp.Text = "";
        tb_eo.Text = "";
        tb_license.Text = "";
        tb_mailogin.Text = "";
        tb_phone.Text = "";
        tb_add.Text = "";
        tb_w.Text = "";
        
        PMsg_ModalPopupExtender.Hide();
    }

    private void fillcompanies()
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getCompanies();
          

            if (!rxml.Equals("") || rxml != null)
            {
                cbcompany.Items.Clear();
                DataSet dsclient = Utility.getCompanies(rxml);
                ListItem listitem0 = new ListItem();
                listitem0.Text = "Select One"; listitem0.Value = "";
                cbcompany.Items.Add(listitem0);

                gv_company.DataSource = dsclient;
                gv_company.DataBind();

               

                for (int ii = 0; ii <= (dsclient.Tables[0].Rows.Count - 1); ii++)
                {
                    ListItem listitem = new ListItem();
                    listitem.Text = dsclient.Tables[0].Rows[ii][1].ToString();
                    listitem.Value = dsclient.Tables[0].Rows[ii][0].ToString();
                    cbcompany.Items.Add(listitem);


                  


                }

                
               
            }

        }
        catch (Exception ee)
        {
            Utility.writeError(ee.Message + ee.StackTrace);

        }
    }

    private void fillaccordion(DataTable dt)
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getCompanies();


            if (!rxml.Equals("") || rxml != null)
            {
               
                DataSet dsclient = Utility.getCompanies(rxml);
                ListItem listitem0 = new ListItem();
                listitem0.Text = "Select One"; listitem0.Value = "";
               
                gv_company.DataSource = dsclient;
                gv_company.DataBind();

                AjaxControlToolkit.Accordion accordion = new AjaxControlToolkit.Accordion();
                accordion.ID = "accordion";
                accordion.RequireOpenedPane = false;
                accordion.SelectedIndex = -1;
                accordion.HeaderCssClass = "accordion3";
                accordion.Width = 900;
                accordion.HeaderSelectedCssClass = "accordionSelected3";
                accordion.ContentCssClass = "accordionContent3";
                

                for (int ii = 0; ii <= (dsclient.Tables[0].Rows.Count - 1); ii++)
                {
                    ListItem listitem = new ListItem();
                    listitem.Text = dsclient.Tables[0].Rows[ii][1].ToString();
                    listitem.Value = dsclient.Tables[0].Rows[ii][0].ToString();
                    cbcompany.Items.Add(listitem);


                    AjaxControlToolkit.AccordionPane pane = new AjaxControlToolkit.AccordionPane();
                    pane.ID = "acc_" + listitem.Value;
                    string url = "";
                    string phone = "";
                    string contacts = "";
                    if (!dsclient.Tables[0].Rows[ii][2].ToString().Equals("")) url = " - "+ dsclient.Tables[0].Rows[ii][2].ToString();
                    if (!dsclient.Tables[0].Rows[ii][3].ToString().Equals("")) phone = " - " + dsclient.Tables[0].Rows[ii][3].ToString();
                    if (!dsclient.Tables[0].Rows[ii][4].ToString().Equals("")) contacts = " - " + dsclient.Tables[0].Rows[ii][4].ToString();

                    pane.HeaderContainer.Controls.Add(new LiteralControl(listitem.Text + url + phone+contacts));
                    Table tabla = crearTablaCuentas(dt, listitem.Value);
                    tabla.Attributes.Add("margin-left", "200px");
                    pane.ContentContainer.Controls.Add(tabla);
                    
                    accordion.Panes.Add(pane);


                }

                Panel1.Controls.Add(accordion);

            }

        }
        catch (Exception ee)
        {
            Utility.writeError(ee.Message + ee.StackTrace);

        }
    }

    private Table crearTablaCuentas(DataTable dsclient, string companyid)
    {
        Table result = new Table();
        
        for (int j = 0; j <= (dsclient.Rows.Count - 1); j++)
        {
            string temp = dsclient.Rows[j][13].ToString();
            if (dsclient.Rows[j][13].ToString().Equals(companyid))
            {
                TableRow tr = new TableRow();
                tr.CssClass = "letracombo";
                tr.Height= 25;
                TableCell td = new TableCell();
                TableCell td1 = new TableCell();
                TableCell td2 = new TableCell();
                TableCell td3 = new TableCell();
                ImageButton im = new ImageButton();
                im.ID = "img_" + dsclient.Rows[j][0].ToString();
                im.ImageUrl = "~/img/icon_content_small.gif";
                im.Width = 15;
                im.Click += new ImageClickEventHandler(edit);
                td1.Text = dsclient.Rows[j][2].ToString();
                td2.Text = dsclient.Rows[j][3].ToString();
                td3.Text = dsclient.Rows[j][4].ToString();
                td.Controls.Add(im);
                tr.Cells.Add(td);
                tr.Cells.Add(td1);
                tr.Cells.Add(td2);
                tr.Cells.Add(td3);
                result.Rows.Add(tr);

            }

        }

       

        return result;


    }

    protected void lnk_msg_ok1_Click(object sender, EventArgs e)
    {
        img_error.Visible = false;
        img_ok.Visible = false;
        lb_error.Visible = false;
        ArrayList array = new ArrayList();
        RE_GenericBean rg = new RE_GenericBean();
        rg.intC1 = int.Parse(lbid.Text);
        rg.intC2 = int.Parse(cbcompany.SelectedValue);
        rg.intC3 = int.Parse(DropDownList1.SelectedValue);
        
        rg.strC3 = tb_user.Text;
        rg.strC4 = tb_pass.Text;
        rg.strC5 = tb_mailogin.Text;
        rg.strC6 = tb_add.Text;
        rg.strC7 = tb_phone.Text;
        rg.strC8 = tb_license.Text;
        rg.strC9 = tb_exp.Text;
        rg.strC10 = tb_eo.Text;
        rg.strC11 = tb_exp2.Text;
        rg.strC12 = tb_w.Text;

        array.Add(rg);
        RE_GenericBean resop = new RE_GenericBean();
        string user = "";

        if (Session["userid"] == null)
        {
            ruser = (rUser)Session["userid"];
            user = ruser.getAlias();

        }
        resop = DB.saveLogin(array, user);
        if (resop.intC1 == 1)
        {
            
            img_ok.Visible = true;
            lb_error.Visible = true;
            lb_error.Text = "Login Added";

        }
        else {
            img_error.Visible = true;
            lb_error.Visible = true;
            lb_error.Text = "Login not Added";
        
        }
        Loginarr = (DataTable)DB.getArrLoginAcc("");
        fillaccordion(Loginarr);

        cbcompany.SelectedIndex = 0;
       
        tb_user.Text = "";
        tb_pass.Text = "";
        tb_mailogin.Text = "";
        tb_add.Text = "";
        tb_phone.Text = "";
        tb_license.Text = "";
        tb_exp.Text = "";
        tb_eo.Text = "";
        tb_exp2.Text = "";
        tb_w.Text = "";
        lbid.Text = "0";
        Response.Redirect("Companylist.aspx");
    }
    protected void LinkButton2_Click1(object sender, EventArgs e)
    {

    }
    protected void LinkButton2_Click2(object sender, EventArgs e)
    {
        ModalPopupExtender1.Hide();

    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {//agregamdp compania
        lb_error.Text = "";
        img_error.Visible = false;
        img_ok.Visible = false;
        if (!txtcompany.Text.Equals(""))
        {

            string sxml = "";
            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><name>";
            txtcompany.Text= txtcompany.Text.Replace("&", "&amp;");
            sxml = sxml + txtcompany.Text;
            txturl.Text= txturl.Text.Replace("&", "&amp;");
            sxml = sxml + "</name><estado>1</estado><url>" + txturl.Text + "</url>";
            tb_contacts.Text=tb_contacts.Text.Replace("&", "&amp;");
            tb_phone2.Text=tb_phone2.Text.Replace("&", "&amp;");
            sxml = sxml + "<contacts>"+tb_contacts.Text+"</contacts><phone>"+tb_phone2.Text+"</phone><id>"+lbidc.Text+"</id></datos_registro>";

            int resp;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            resp = service.RegistraCompany(sxml);
            if (resp == 1)
            {
                fillcompanies();
                
                txtcompany.Text = "";
                txturl.Text = "";
                tb_phone2.Text = "";
                tb_contacts.Text = "";
                lbidc.Text = "0";

            }
            else
            {
                lb_error.Text = "Company no added. Try Again.";
                lb_error.Visible = true;
                img_ok.Visible = false;
                img_error.Visible = true;
            }
            service.Dispose();
        }
        else
        {
            lb_error.Text = "Invalid name for new Company";
            lb_error.Visible = true;
            img_error.Visible = true;
        }
    }
    
    protected void ln_deletecomp_Click(object sender, EventArgs e)
    {

        RE_GenericBean rg = new RE_GenericBean();
        rg.intC1 = int.Parse(lbidc.Text);
        int resp = DB.deleteCompany(rg.intC1);
        if (resp == 1)
        {
            lb_error.Text = "Company Deleted";
            lb_error.Visible = true;
            lb_error.ForeColor = System.Drawing.Color.Blue;
            img_ok.Visible = true;
        }
        else
        {
            lb_error.Text = "Cant be delete   ";
            lb_error.Visible = true;
            lb_error.ForeColor = System.Drawing.Color.Red;
            img_error.Visible = false;
        }



        Loginarr = (DataTable)DB.getArrLoginAcc("");
        fillaccordion(Loginarr);
        cbcompany.SelectedIndex = 0;
       
        tb_user.Text = "";
        tb_pass.Text = "";
        tb_mailogin.Text = "";
        tb_add.Text = "";
        tb_phone.Text = "";
        tb_license.Text = "";
        tb_exp.Text = "";
        tb_eo.Text = "";
        tb_exp2.Text = "";
        tb_w.Text = "";
        lbid.Text = "0";
        Response.Redirect("companylist.aspx");
    }
    protected void lncancelag_Click(object sender, EventArgs e)
    {
        ModalPopupExtender2.Hide();
    }
    protected void lnsaveagg_Click(object sender, EventArgs e)
    {
        img_error.Visible = false;
        img_ok.Visible = false;
        lb_error.Visible = false;
        RE_GenericBean rg = new RE_GenericBean();
        rg.intC1 = int.Parse(lbidagent.Text);
        rg.strC1 = tbnameag.Text;
        rg.strC2 = tbemailag.Text;
        ArrayList array= new ArrayList();
        array.Add(rg);
        RE_GenericBean resop = new RE_GenericBean();
        string user = "";

        if (Session["userid"] == null)
        {
            ruser = (rUser)Session["userid"];
            user = ruser.getAlias();

        }
        resop = DB.saveAgent(array, user);
        if (resop.intC1 == 1)
        {

            img_ok.Visible = true;
            lb_error.Visible = true;
            lb_error.Text = "Agent Added";

        }
        else
        {
            img_error.Visible = true;
            lb_error.Visible = true;
            lb_error.Text = "Agent not Added";

        }
        Loginarr = (DataTable)DB.getArrLoginAcc("");
        fillaccordion(Loginarr);

        tbnameag.Text="";
        tbemailag.Text="";
        Response.Redirect("Companylist.aspx");
    }
    protected void lndeleteag_Click(object sender, EventArgs e)
    {
        RE_GenericBean rg = new RE_GenericBean();
        int id = int.Parse(lbidagent.Text);
        int resp = DB.deleteAgent(id);
        if (resp == 1)
        {
            lb_error.Text = "Agent Deleted";
            lb_error.Visible = true;
            lb_error.ForeColor = System.Drawing.Color.Blue;
            img_ok.Visible = true;
        }
        else
        {
            lb_error.Text = "Agent cant be delete   ";
            lb_error.Visible = true;
            lb_error.ForeColor = System.Drawing.Color.Red;
            img_error.Visible = false;
        }
                
        tbemailag.Text = "";
        tbnameag.Text = "";
        
        
        lbidagent.Text = "0";
        Response.Redirect("companylist.aspx");
    }
    protected void ln_viewagent_Click(object sender, EventArgs e)
    {
        if (viewag.Text.Equals("1"))
        {
            pagent.Visible = false;
            pcompany.Visible = true;
            viewag.Text = "0";
            ln_viewagent.Text = "<span>View Agent</span>";
        }
        else {
            pagent.Visible = true;
            pcompany.Visible = false;
            viewag.Text = "1";
            ln_viewagent.Text = "<span>View Company</span>";
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.SelectedValue = DropDownList1.SelectedValue;
        tb_mailogin.Text = DropDownList2.SelectedItem.ToString();
        PMsg_ModalPopupExtender.Show();
    }
}